package de.slothsoft.wasbinich.android;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import de.slothsoft.wasbinich.android.util.ImageUtil;
import de.slothsoft.wasbinich.core.Picture;
import de.slothsoft.wasbinich.core.util.PictureUtil;

public class AnswerActivity extends AbstractActivity implements OnClickListener {

    public static final String ARG_ANSWER = "answer"; //$NON-NLS-1$
    public static final String ARG_PICTURE = "picture"; //$NON-NLS-1$

    private Picture picture;
    private CharSequence insertedAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_answer);

	this.insertedAnswer = getIntent().getCharSequenceExtra(ARG_ANSWER);
	this.picture = (Picture) getIntent().getSerializableExtra(ARG_PICTURE);

	updateControlsFromPicture();
	hookOnClickListener();
    }

    private void updateControlsFromPicture() {
	ImageView imageSectionView = (ImageView) findViewById(R.id.answer_imageSection);

	Bitmap bigPicture = ImageUtil.createBigPictureBitmap(this.picture);
	imageSectionView.setImageBitmap(bigPicture == null ? ImageUtil.createSectionBitmap(this.picture) : bigPicture);

	TextView correctAnswer = (TextView) findViewById(R.id.answer_correct_answer);
	correctAnswer.setText(this.picture.correctAnswers[0]);

	TextView alternativeAnswers = (TextView) findViewById(R.id.answer_alternative_answers);
	alternativeAnswers.setText(PictureUtil.createAlternativeAnswersString(this.picture.correctAnswers));

	TextView description = (TextView) findViewById(R.id.answer_description);
	description.setText(this.picture.description);

	if (this.insertedAnswer != null) {
	    TextView title = (TextView) findViewById(R.id.answer_title);
	    title.setText(PictureUtil.isCorrectAnswer(this.picture, this.insertedAnswer.toString()) ? R.string.answer_correct
		    : R.string.answer_wrong);
	}
    }

    private void hookOnClickListener() {
	View parentView = findViewById(R.id.layout);
	parentView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
	setResult(RESULT_OK);
	finish();
    }
}
